//index.js
//获取应用实例
const app = getApp()
var config = require('../../utils/config.js')
Page({
  data: {
    notices: [],
    lbt: [],
    preLbt: [],
    pageNum: 1,
    pageSize: 10,
    order: 0,
    isLastPage: false,
    msg: '上拉加载更多',
    status: 0
  },
  onLoad() {
    this._getLbt()
    this._getCategory()
    this._getNotices(0)
  },
  _getCategory() {
    let _this = this
    // 展示的分类数组
    let categ = [];
    let index = 0;
    // 获取分类数组
    wx.request({
      url: config.HOST + '/category/listCategory',
      success(res) {
        for (let i = 0; i < res.data.length; i++) {
          // 满8个，另起一页
          index = parseInt(i / 8)
          if (categ[index] == undefined) {
            categ[index] = [];
          }
          categ[index].push(res.data[i])
        }
        _this.setData({
          categ
        })
        console.log(categ)
      }
    })
  },
  // 获取首页上的公告
  _getNotices(status) {
    let _this = this
    wx.request({
      url: config.HOST + '/notice/indexNotices',
      data: {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
        order: this.data.order,
      },
      success(res) {
        console.log(res.data)
        let data = res.data;

        if (data.data.list.length == 0) {
          _this.setData({
            msg: '没有公告'
          })
        }
        if (data.data.isLastPage == true) {
          _this.setData({
            msg: '加载完毕'
          })
        }

        if (status == 0) { // 0是上拉刷新，不要以前的数据
          _this.data.notices = data.data.list
        } else {
          _this.data.notices = _this.data.notices.concat(data.data.list)
        }
        _this.setData({
          notices: _this.data.notices,
          isLastPage: data.data.isLastPage
        })
        //页数加1
        _this.data.pageNum++;
        wx.stopPullDownRefresh()
      }
    })
  },
  _getLbt() {
    let _this = this
    wx.request({
      url: config.HOST + '/notice/listNoticeForIndex',
      success(res) {
        console.log("轮播图")
        res.data.forEach((item, index) => {
          if (item.img != null && item.img != '') {
            item.img = item.img.split(',')
          }
          _this.data.preLbt.push(item.img[0])
          console.log(item)
        })
        _this.setData({
          lbt: res.data
        })
      }
    })
  },
  preLbt(event) {
    let img = event.target.dataset.img
    wx.previewImage({
      urls: this.data.preLbt,
      current: img,
    })
  },
  searchPage() {
    wx.navigateTo({
      url: '../search/index',
    })
  },
  userCenter() {
    // 进入用户中心判断用户身份登录
    app.login().then(() => {
        console.log("登陆了")
        wx.navigateTo({
          url: '../user/index',
        })
      })
      .catch((data) => {
        console.log(data)
        wx.navigateTo({
          url: '../login/index',
        })
      })
  },
  chooseCategory() {
    app.login().then(() => { //
        console.log("登陆了")
        wx.navigateTo({
          url: '../choose_category/index',
        })
      })
      .catch((data) => {
        console.log(data)
        wx.navigateTo({
          url: '../login/index',
        })
      })
  },
  //切换最新和最热
  changeStataus(e) {
    let order = e.target.dataset.status
    this.setData({
      order,
      notices: []
    })
    this.data.pageNum = 1
    this._getNotices(0)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.data.pageNum = 1
    this.setData({
      notices: []
    })
    this._getLbt()
    this._getCategory()
    this._getNotices(0);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.isLastPage == true) {
      this.setData({
        msg: '我是有底线的'
      })
      return;
    }

    console.log("下拉刷新")
    this._getNotices();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: '公众墙',
      path: this.route ,
      // imageUrl: '这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete: function (res) {
        // 不管成功失败都会执行
      }
    }

  }
})